package servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import po.Account;
import po.Response;
import util.ResponseUtil;

/**
 * Servlet implementation class CartServlet
 */
public class CartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		if (action.equals("add")) {
			addCart(request, response);
		} else if (action.equals("update")) {
			updateCart(request, response);
		}else if(action.equals("delete")){
			deleteCart(request,response);
		}
	}

	private void deleteCart(HttpServletRequest request, HttpServletResponse response) {
		String did = request.getParameter("did");
		Response res = new Response();
		HttpSession session = request.getSession();
		Account account = (Account) session.getAttribute("account");
		Map<Integer, Integer> carts = (Map<Integer, Integer>) session.getAttribute("carts");
		if(carts.containsKey(did)){
			carts.remove(did);
			res.setStatus(true);
			res.setMessage("删除成功");
			if(account != null){
				ServletContext servletContext = request.getServletContext();
				servletContext.setAttribute(account.getAid(), carts);
			}
			session.setAttribute("carts", carts);
			
		}else{
			res.setStatus(false);
			res.setMessage("没找到该数据");
		}
		try {
			ResponseUtil.write(res, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void updateCart(HttpServletRequest request, HttpServletResponse response) {
		String did = request.getParameter("did");
		String count = request.getParameter("count");
		HttpSession session = request.getSession();
		Account account = (Account) session.getAttribute("account");
		Map<Integer, Integer> carts = (Map<Integer, Integer>) session.getAttribute("carts");
		if(carts == null)
			carts = new HashMap<Integer,Integer>();
		System.out.println("before======================" + carts.get(Integer.valueOf(did)));
		if(Integer.valueOf(count) <= 0){
			carts.remove(Integer.valueOf(did));
		}else{
			System.out.println("after======================" + carts.get(Integer.valueOf(did)));
			carts.put(Integer.valueOf(did),Integer.valueOf(count));	
		}
		
		if(account != null){
			ServletContext servletContext = request.getServletContext();
			servletContext.setAttribute(account.getAid(), carts);
		}
		
		session.setAttribute("carts", carts);
		Response res = new Response();
		res.setStatus(true);
		res.setMessage("修改数量成功");
		try {
			ResponseUtil.write(res, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void addCart(HttpServletRequest request, HttpServletResponse response) {
		String did = request.getParameter("did");
		HttpSession session = request.getSession();
		Account account = (Account) session.getAttribute("account");
		Response res = new Response();
		Map<Integer, Integer> carts = (Map<Integer, Integer>) session.getAttribute("carts");
		if (carts == null) {
			carts = new HashMap<Integer, Integer>();
		}
		System.out.println("before======================" + carts.get(Integer.valueOf(did)));
		if (carts.containsKey(Integer.valueOf(did))) {
			int number = carts.get(Integer.valueOf(did));
			carts.put(Integer.valueOf(did), number + 1);
			res.setData("secondAdd");
		} else {
			carts.put(Integer.valueOf(did), 1);
			res.setData("firstAdd");
		}
		
		if(account != null){
			ServletContext servletContext = request.getServletContext();
			servletContext.setAttribute(account.getAid(), carts);
		}
		session.setAttribute("carts", carts);
		System.out.println("after======================" + carts.get(Integer.valueOf(did)));
		
		res.setStatus(true);
		res.setMessage("添加成功");
		try {
			ResponseUtil.write(res, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
	
}
